home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / Sample Code / Sample Editors⁄Viewers / Cappuccino / Source / Action.cpp next >
Encoding:
Text File  |  1995-12-08  |  13.8 KB  |  484 lines  |  [TEXT/MPS ]

  1. /*
  2.  
  3.     File:        CappuccinoUndo.cpp
  4.     
  5.     
  6.     Contents:    CappuccinoUndo implements undo/redo support for
  7.                 the Cappuccino part.
  8.     
  9.     Written by:    Troy Gaul
  10.     
  11.     Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  12.     
  13. */
  14.  
  15. // -- Compiler/Preprocessor Switches --
  16.  
  17. #ifndef _COMPILERDEFS_
  18. #include "CompDefs.h"
  19. #endif
  20.  
  21. // -- OpenDoc Utilities --
  22.  
  23. #ifndef _EXCEPT_
  24. // Exceptions define several important macros (eg. CHECKENV)
  25. // which are used in the SOM method dispatch glue. If Except.h
  26. // is not included early enough, exceptions may not be thrown
  27. // correctly when returning from a SOM method with the "ev" parameter set.
  28. #include <Except.h>
  29. #endif
  30.  
  31. // -- Cappuccino Includes --
  32.  
  33. #ifndef _ACTION_
  34. #include "Action.h"
  35. #endif
  36.  
  37. #ifndef _CAPPUCCINO_
  38. #include "Cappuccino.h"
  39. #endif
  40.  
  41. #ifndef _CAPPUCCINOCONTENT_
  42. #include "CappuccinoContent.h"
  43. #endif
  44.  
  45. #ifndef _CAPPUCCINODEF_
  46. #include "CappuccinoDef.h"
  47. #endif
  48.  
  49. #ifndef _CAPPUCCINOGLOBALS_
  50. #include "CappuccinoGlobals.h"
  51. #endif
  52.  
  53. // -- OpenDoc Includes --
  54.  
  55. #ifndef _ODTYPES_
  56. #include <ODTypes.h>
  57. #endif
  58.  
  59. #ifndef SOM_ODClipboard_xh
  60. #include <Clipbd.xh>
  61. #endif
  62.  
  63. #ifndef SOM_ODDragItemIterator_xh
  64. #include <DgItmIt.xh>
  65. #endif
  66.  
  67. #ifndef SOM_ODDragAndDrop_xh
  68. #include <DragDrp.xh>
  69. #endif
  70.  
  71. #ifndef SOM_ODUndo_xh
  72. #include <Undo.xh>
  73. #endif
  74.  
  75. // -- OpenDoc Utilities --
  76.  
  77. #ifndef _BARRAY_
  78. #include <BArray.h>
  79. #endif
  80.  
  81. #ifndef _FOCUSLIB_
  82. #include <FocusLib.h>
  83. #endif
  84.  
  85. #ifndef _ODDEBUG_
  86. #include <ODDebug.h>
  87. #endif
  88.  
  89. #ifndef _ODUTILS_
  90. #include <ODUtils.h>
  91. #endif
  92.  
  93. #ifndef _PASCLSTR_
  94. #include <PasclStr.h>
  95. #endif
  96.  
  97. #ifndef _TEMPOBJ_
  98. #include <TempObj.h>
  99. #endif
  100.  
  101. #ifndef _USERSRCM_
  102. #include <UseRsrcM.h>
  103. #endif
  104.  
  105. //==============================================================================
  106. // CAction
  107. //==============================================================================
  108.  
  109. //------------------------------------------------------------------------------
  110. // Method:        Constructor
  111. // Origin:        CAction
  112. //------------------------------------------------------------------------------
  113.  
  114. CAction::CAction( Cappuccino* part, ActionCode actionCode )
  115. {
  116.     fPart = part;
  117.     fActionCode = actionCode;
  118. }
  119.  
  120. //------------------------------------------------------------------------------
  121. // Method:        Destructor
  122. // Origin:        CAction
  123. //------------------------------------------------------------------------------
  124.  
  125. CAction::~CAction()
  126. {
  127. }
  128.     
  129. //------------------------------------------------------------------------------
  130. // Method:        Do
  131. // Origin:        CAction
  132. //------------------------------------------------------------------------------
  133.  
  134. void CAction::Do( Environment* ev )
  135. {
  136.     this->AddToActionHistory(ev);
  137. }
  138.  
  139. //------------------------------------------------------------------------------
  140. // Method:        Undo
  141. // Origin:        CAction
  142. //------------------------------------------------------------------------------
  143.  
  144. void CAction::Undo( Environment* ev )
  145. {
  146. }
  147.  
  148. //------------------------------------------------------------------------------
  149. // Method:        Redo
  150. // Origin:        CAction
  151. //------------------------------------------------------------------------------
  152.  
  153. void CAction::Redo( Environment* ev )
  154. {
  155. }
  156.  
  157. //------------------------------------------------------------------------------
  158. // Method:        GetActionName
  159. // Origin:        CAction
  160. //------------------------------------------------------------------------------
  161.  
  162. ODName* CAction::GetActionName( Environment* ev )
  163. {
  164.     Str255 nameStr;
  165.     ODGetIndString(nameStr, kActionStringResID, fActionCode);
  166.     
  167.     return CreateITextPString(gGlobals->fEditorsScript, 
  168.                               gGlobals->fEditorsLanguage, 
  169.                               nameStr);
  170. }
  171.  
  172. //------------------------------------------------------------------------------
  173. // Method:        GetUndoRedoName
  174. // Origin:        CAction
  175. //------------------------------------------------------------------------------
  176.  
  177. const ODBoolean kUndoName = kODTrue;
  178. const ODBoolean kRedoName = kODFalse;
  179.  
  180. ODName* CAction::GetUndoRedoName( Environment*        ev,
  181.                                   ODBoolean            isUndo )
  182. {
  183.     CUsingLibraryResources res;
  184.     
  185.     ODUShort startIndex = (isUndo) ? kUndoPrefixIndex : kRedoPrefixIndex;
  186.     
  187.     // Start withe the prefix.
  188.     Str255 nameStr;
  189.     GetIndString(nameStr, kUndoStringResID, startIndex);
  190.     
  191.     // Concat the action code's name.
  192.     Str255 appendStr;
  193.     
  194.     TempODName actionName = this->GetActionName(ev);
  195.     GetITextPString(actionName, appendStr);
  196.     ConcatPascalStrings(nameStr, appendStr);
  197.     
  198.     // Concat the suffix.
  199.     GetIndString(appendStr, kUndoStringResID, startIndex + 1);
  200.     ConcatPascalStrings(nameStr, appendStr);
  201.     
  202.     return CreateITextPString(gGlobals->fEditorsScript, 
  203.                               gGlobals->fEditorsLanguage, 
  204.                               nameStr);
  205. }
  206.  
  207. //------------------------------------------------------------------------------
  208. // Method:        AddToActionHistory
  209. // Origin:        CAction
  210. //------------------------------------------------------------------------------
  211.  
  212. void CAction::AddToActionHistory( Environment* ev, ODActionType actionType )
  213. {
  214.     SCappuccionActionState state;
  215.     state.fAction = this;
  216.     
  217.     // Get the undo and redo menu item strings.
  218.     TempODName undoName = this->GetUndoRedoName(ev, kUndoName);
  219.     TempODName redoName = this->GetUndoRedoName(ev, kRedoName);
  220.  
  221.     // Stuff our action data into a ByteArray and add it to the action history.
  222.     ODUndo* undo = ODGetSession(ev, fPart->GetODPart())->GetUndo(ev);
  223.     TempODActionData actionData = CreateByteArray(&state, sizeof state);
  224.     
  225.     undo->AddActionToHistory(ev, fPart->GetODPart(), actionData, 
  226.                              actionType, undoName, redoName);
  227. }
  228.  
  229. //==============================================================================
  230. // CTextChangeAction
  231. //==============================================================================
  232.  
  233. //------------------------------------------------------------------------------
  234. // Method:        Constructor
  235. // Origin:        CTextChangeAction
  236. //------------------------------------------------------------------------------
  237.  
  238. CTextChangeAction::CTextChangeAction( Cappuccino*            part, 
  239.                                       ActionCode            actionCode,
  240.                                       CCappuccinoContent*    newContent)
  241.     : CAction(part, actionCode)
  242. {
  243.     fCurrentContent  = kODNULL;
  244.     fPreviousContent = kODNULL;
  245.     
  246.     if (newContent)
  247.         newContent->Acquire();
  248.     fCurrentContent = newContent;
  249. }
  250.  
  251. //------------------------------------------------------------------------------
  252. // Method:        Destructor
  253. // Origin:        CTextChangeAction
  254. //------------------------------------------------------------------------------
  255.  
  256. CTextChangeAction::~CTextChangeAction()
  257. {
  258.     if (fPreviousContent)
  259.         fPreviousContent->Release();
  260.     if (fCurrentContent)
  261.         fCurrentContent->Release();
  262. }
  263.  
  264. //------------------------------------------------------------------------------
  265. // Method:        Do
  266. // Origin:        CTextChangeAction
  267. //------------------------------------------------------------------------------
  268.  
  269. void CTextChangeAction::Do( Environment* ev )
  270. {
  271.     // Get the previous content at the time the action is performed.
  272.     fPreviousContent = fPart->GetContent();
  273.     if (fPreviousContent)
  274.         fPreviousContent->Acquire();
  275.     
  276.     Inherited::Do(ev);
  277.     
  278.     fPart->SetContent(ev, fCurrentContent);
  279. }
  280.  
  281. //------------------------------------------------------------------------------
  282. // Method:        Undo
  283. // Origin:        CTextChangeAction
  284. //------------------------------------------------------------------------------
  285.  
  286. void CTextChangeAction::Undo( Environment* ev )
  287. {
  288.     fPart->SetContent(ev, fPreviousContent);
  289. }
  290.  
  291. //------------------------------------------------------------------------------
  292. // Method:        Redo
  293. // Origin:        CTextChangeAction
  294. //------------------------------------------------------------------------------
  295.  
  296. void CTextChangeAction::Redo( Environment* ev )
  297. {
  298.     fPart->SetContent(ev, fCurrentContent);
  299. }
  300.  
  301. //==============================================================================
  302. // CClipboardTextChangeAction
  303. //==============================================================================
  304.  
  305. //------------------------------------------------------------------------------
  306. // Method:        Constructor
  307. // Origin:        CClipboardTextChangeAction
  308. //------------------------------------------------------------------------------
  309.  
  310. CClipboardTextChangeAction::CClipboardTextChangeAction( Cappuccino*            part, 
  311.                                                         ActionCode            actionCode,
  312.                                                         ODCloneKind            cloneKind,
  313.                                                         CCappuccinoContent*    content)
  314.     : CTextChangeAction(part, actionCode, content)
  315. {
  316.     fCloneKind = cloneKind;
  317.     fUpdateID = kODUnknownUpdate;
  318. }
  319.  
  320. //------------------------------------------------------------------------------
  321. // Method:        Destructor
  322. // Origin:        CClipboardTextChangeAction
  323. //------------------------------------------------------------------------------
  324.  
  325. CClipboardTextChangeAction::~CClipboardTextChangeAction()
  326. {
  327. }
  328.  
  329. //------------------------------------------------------------------------------
  330. // Method:        Do
  331. // Origin:        CClipboardTextChangeAction
  332. //------------------------------------------------------------------------------
  333.  
  334. void CClipboardTextChangeAction::Do( Environment* ev )
  335. {
  336.     ODSession* session = ODGetSession(ev, fPart->GetODPart());
  337.     fUpdateID = session->GetClipboard(ev)->ActionDone(ev, fCloneKind);
  338.     Inherited::Do(ev);
  339. }
  340.  
  341. //------------------------------------------------------------------------------
  342. // Method:        Undo
  343. // Origin:        CClipboardTextChangeAction
  344. //------------------------------------------------------------------------------
  345.  
  346. void CClipboardTextChangeAction::Undo( Environment* ev )
  347. {
  348.     Inherited::Undo(ev);
  349.     
  350.     // Let the clipboard know.
  351.     WASSERT(fUpdateID != kODUnknownUpdate);
  352.     ODSession* session = ODGetSession(ev, fPart->GetODPart());
  353.     session->GetClipboard(ev)->ActionUndone(ev, fUpdateID, fCloneKind);
  354. }
  355.  
  356. //------------------------------------------------------------------------------
  357. // Method:        Redo
  358. // Origin:        CClipboardTextChangeAction
  359. //------------------------------------------------------------------------------
  360.  
  361. void CClipboardTextChangeAction::Redo( Environment* ev )
  362. {
  363.     Inherited::Redo(ev);
  364.  
  365.     // Let the clipboard know.
  366.     WASSERT(fUpdateID != kODUnknownUpdate);
  367.     ODSession* session = ODGetSession(ev, fPart->GetODPart());
  368.     session->GetClipboard(ev)->ActionRedone(ev, fUpdateID, fCloneKind);
  369. }
  370.  
  371. //==============================================================================
  372. // CCutAction
  373. //==============================================================================
  374.  
  375. //------------------------------------------------------------------------------
  376. // Method:        Constructor
  377. // Origin:        CCutAction
  378. //------------------------------------------------------------------------------
  379.  
  380. CCutAction::CCutAction( Cappuccino* part )
  381.     : CClipboardTextChangeAction(part, kActionCut, kODCloneCut, kODNULL)
  382. {
  383.     Environment* ev = somGetGlobalEnvironment();    // ••• make param?
  384.  
  385.     fCurrentContent = new CCappuccinoContent;
  386.     fCurrentContent->InitCappuccinoContent(ev);
  387. }
  388.  
  389. //==============================================================================
  390. // CPasteAction
  391. //==============================================================================
  392.  
  393. //------------------------------------------------------------------------------
  394. // Method:        Constructor
  395. // Origin:        CPasteAction
  396. //------------------------------------------------------------------------------
  397.  
  398. CPasteAction::CPasteAction( Cappuccino* part, CCappuccinoContent* content )
  399.     : CClipboardTextChangeAction(part, kActionPaste, kODClonePaste, content)
  400. {
  401. }
  402.  
  403. //==============================================================================
  404. // CClearAction
  405. //==============================================================================
  406.  
  407. //------------------------------------------------------------------------------
  408. // Method:        Constructor
  409. // Origin:        CClearAction
  410. //------------------------------------------------------------------------------
  411.  
  412. CClearAction::CClearAction( Cappuccino* part )
  413.     : CTextChangeAction(part, kActionClear, kODNULL)
  414. {
  415.     Environment* ev = somGetGlobalEnvironment();    // ••• make param?
  416.  
  417.     fCurrentContent = new CCappuccinoContent;
  418.     fCurrentContent->InitCappuccinoContent(ev);
  419. }
  420.  
  421. //==============================================================================
  422. // CDropAction
  423. //==============================================================================
  424.  
  425. //------------------------------------------------------------------------------
  426. // Method:        Constructor
  427. // Origin:        CDropAction
  428. //------------------------------------------------------------------------------
  429.  
  430. CDropAction::CDropAction( Cappuccino* part, CCappuccinoContent* content )
  431.     : CTextChangeAction(part, kActionDrop, content)
  432. {
  433. }
  434.  
  435. //==============================================================================
  436. // CDragBeginAction
  437. //==============================================================================
  438.  
  439. //------------------------------------------------------------------------------
  440. // Method:        Constructor
  441. // Origin:        CDragBeginAction
  442. //------------------------------------------------------------------------------
  443.  
  444. CDragBeginAction::CDragBeginAction( Cappuccino* part )
  445.     : CAction(part, kActionDrag)
  446. {
  447. }
  448.  
  449. //------------------------------------------------------------------------------
  450. // Method:        Do
  451. // Origin:        CDragBeginAction
  452. //------------------------------------------------------------------------------
  453.  
  454. void CDragBeginAction::Do( Environment* ev )
  455. {
  456.     this->AddToActionHistory(ev, kODBeginAction);
  457.     // Don't call Inherited as we're replacing its functionality.
  458. }
  459.  
  460. //==============================================================================
  461. // CDragEndAction
  462. //==============================================================================
  463.  
  464. //------------------------------------------------------------------------------
  465. // Method:        Constructor
  466. // Origin:        CDragEndAction
  467. //------------------------------------------------------------------------------
  468.  
  469. CDragEndAction::CDragEndAction( Cappuccino* part )
  470.     : CAction(part, kActionDrag)
  471. {
  472. }
  473.  
  474. //------------------------------------------------------------------------------
  475. // Method:        Do
  476. // Origin:        CDragEndAction
  477. //------------------------------------------------------------------------------
  478.  
  479. void CDragEndAction::Do( Environment* ev )
  480. {
  481.     this->AddToActionHistory(ev, kODEndAction);
  482.     // Don't call Inherited as we're replacing its functionality.
  483. }
  484.